<?php
// site root
$sSiteRoot = '.';
define("SITE_ROOT", realpath(dirname(__FILE__) . '/' . $sSiteRoot));


// parameters
$sMethod = @$_GET['m'];
$sFormat = (isset($_GET['f'])?strtolower($_GET['f']):'json');
$GLOBALS['_ObjectResponse'] = ResponseFactory::create($sFormat);
$sDataStore = (isset($_GET['ds'])?$_GET['ds']:'');
if(!$sDataStore) {
    print $GLOBALS['_ObjectResponse']->getErrorResponse(102, "Param 'ds' is missing");
    exit(1);
}


// import config
$sCustomConfigPath = SITE_ROOT . '/../opt/simaqian.php';
if(file_exists($sCustomConfigPath)) {
    require $sCustomConfigPath;
} else {
    $sCustomConfigPath = SITE_ROOT . '/../etc/simaqian.php';
    if(file_exists($sCustomConfigPath)) {
        require $sCustomConfigPath;
    } else {
        print $GLOBALS['_ObjectResponse']->getErrorResponse(100, "Cannot find config file");
        exit(1);
    }
}

// import lib
require SITE_ROOT . '/../lib/simaqian/libsimaqian.php';
require SITE_ROOT . '/../lib/php_duilie.class.php';
require SITE_ROOT . '/../lib/xapian.php';


// import & run
$sMethodPath = SITE_ROOT . "/methods/$sMethod.php";
if(file_exists($sMethodPath)) {
    require $sMethodPath;
} else {
    print $GLOBALS['_ObjectResponse']->getErrorResponse(101, "Cannot find given method: $sMethod");
    exit(1);
}
?>